﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StockExchanger.Models;
using StockExchanger.Controllers;
using StockExchanger.Repository;


namespace StockExchanger.Models
{

    public class BuySellRepository : IDataBoundRepository, IBuySellRepository
    {
        private StockBuySellDataContext _dataContext;


        StockBuySellDataContext db = new StockBuySellDataContext();

        public BuySellRepository()
        {
            DataSource = new Data();
            _dataContext = new StockBuySellDataContext();
        }
        public BuySellRepository(Data dataSource)
              {
                  DataSource = dataSource;
                    _dataContext = new StockBuySellDataContext();
              }

        public Data DataSource { get; set; }


        public aspnet_User aspnet_User { get; set; }
        public aspnet_User aspnet_User1 { get; set; }


        //
        // Query Methods

        public IQueryable<BuySell> FindAllStock()
        {
            return db.BuySells;
        }

        public IQueryable<BuySell> FindUpcomingStock() {
            return from BuySell in FindAllStock()
                   where BuySell.EndDate > DateTime.Now     // .EventDate
                   orderby BuySell.EndDate
                   select BuySell;
        }

        public IQueryable<BuySell> FindByLocation(float latitude, float longitude)
        {
            var BuySell = from BuySells in FindUpcomingStock()
                          join i in db.BuySells    //NearestStock(latitude, longitude)
                          on BuySells.Id equals i.Id
                          select BuySells;

            return BuySell;
        }

/*        public BuySell GetBuySell(int id)
        {
            return new BuySell();
//            return db.BuySells.SingleOrDefault(d => d.Id == id);
        }
*/
        public IList<BuySell> ListAll()
        {
            var buysells = from m in _dataContext.BuySells
                         select m;
            return buysells.ToList();
        }


        //
        // Insert/Delete Methods

        public void Add(BuySell buySell)
        {


//            if (buySell.SellerId == null)
//             buySell.SellerId = default(System.Guid);
//            if (BuySell.Id == null)
             buySell.Id = Guid.NewGuid();
             buySell.StartDate = DateTime.Now;
             buySell.Sold = false;
//             buySell.SellerId = aspnet_User1.UserName;
                //DataSource.DataContext.BuySells.Where(r => r.ListedBy == this.aspnet_User1.UserName).SingleOrDefault();
            

//            if (buySell.aspnet_User1 == null)
//                buySell.aspnet_User1 = aspnet_User1;

                db.BuySells.InsertOnSubmit(buySell);

//            car.EnsureValid();

//            DataSource.DataContext.BuySells.InsertOnSubmit(buySell);
//            DataSource.DataContext.SubmitChanges();
        }

        public BuySell GetBuySell(int id)
        {
            return db.BuySells.SingleOrDefault(d => d.BuySellId == id);
        }

        public aspnet_User GetUser(string name)
        {
            return db.aspnet_Users.SingleOrDefault(d => d.UserName == name);
        }

        public void Delete(BuySell BuySell)
        {
//            db.RSVPs.DeleteAllOnSubmit(Dse.RSVPs);
//            db.Stock.DeleteOnSubmit(Dse);
            db.BuySells.DeleteOnSubmit(BuySell);
        }

        //
        // Persistence 

        public void Save() {
            db.SubmitChanges();
        }
    }
}
